<?php
/**
 *
 *
 * @author Zeon <scplay@gmail.com>
 * @date 2019/5/28 14:36
 * @copyright PanxSoft Inc.
 */

namespace App\Http\Controllers\App\V1;


use App\Http\Controllers\ApiController;
use App\Http\Requests\App\V1\User\UserUpdateRequest;
use App\Services\OAuth\UserThirdPartyService;
use App\Services\Utils\Guard;
use Illuminate\Http\Request;
// use App\Exceptions\ApiErrCode;

class UserController extends ApiController
{
    const ROUTES = [
        'update' => 'app.v1.user.update',
    ];

    /**
     * 更新用户信息
     *
     * @param UserUpdateRequest $request
     *
     * @return array|\Illuminate\Http\Response
     */
    public function update(UserUpdateRequest $request)
    {
        $user = Guard::user();

        /**
         * 增加birthday自动的更新
         */
        $update_data = array_filter($request->only([
            'nick_name', 'avatar', 'sex', 'language_id','birthday'
        ]));

        $updated = $user->update($update_data);

        (new UserThirdPartyService)->bindRequestOAuthUser($request, $user);

        return $this->response(compact('user', 'updated'));
    }

    /**
     * 根据用户的token获取获取用户的信息
     */
    public function checkoutUserWithToken(Request $request)
    {
        $user = Guard::user();
        return $this->response(compact('user'));
    }
}